package com.net.shop.car.manager.ui.user;

import java.util.Timer;
import java.util.TimerTask;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.net.shop.car.manager.ActivityCollector;
import com.net.shop.car.manager.BaseActivity;
import com.net.shop.car.manager.R;
import com.net.shop.car.manager.api.volley.VolleyCenter;
import com.net.shop.car.manager.api.volley.VolleyListenerImpl;
import com.net.shop.car.manager.api.volley.request.GetRegisterRand;
import com.net.shop.car.manager.api.volley.request.SendPsdRand;
import com.net.shop.car.manager.api.volley.response.GetRegisterRandResponse;
import com.net.shop.car.manager.api.volley.response.SendPsdRandResponse;
import com.net.shop.car.manager.utils.StringAndDateUtils;

public class RegisterActivity extends BaseActivity implements OnClickListener {
	private EditText mEtPhoneNumber;
	private EditText mEtMsgYzm;
	private Button mBtnGetMsgYzm;
	private Button mBtnGoNextActivity;
	private int recLen;
	private Timer timer;
	private TextView province;
	private boolean isLoading;
	private boolean mobileCouldUse;
	private String mobile;
	private String rand;
	private LinearLayout mLlTishi;
	private TextView mLlErrorText;
	private String strMoblile;
	private String strYzm;
	private TextView titleTextView;
	private ImageView title_back_btn;
	private String res;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		initView();
	}

	public void initView() {
		mEtPhoneNumber = (EditText) findViewById(R.id.phone_number_et);
		mEtMsgYzm = (EditText) findViewById(R.id.msg_yzm_et);
		mLlTishi = (LinearLayout) findViewById(R.id.tishi_ll);
		mLlErrorText = (TextView) findViewById(R.id.error_text);
		titleTextView = (TextView) findViewById(R.id.title_txt);
		title_back_btn = (ImageView) findViewById(R.id.title_back_btn);
		res = getIntent().getExtras().getString("res");
		if (res.equals("regist")) {
			titleTextView.setText("注册");
		} else {
			titleTextView.setText("找回密码");
		}

		title_back_btn.setOnClickListener(this);
		mBtnGetMsgYzm = (Button) findViewById(R.id.get_msg_yzm_btn);
		mBtnGoNextActivity = (Button) findViewById(R.id.go_next_activity_btn);
		mBtnGetMsgYzm.setOnClickListener(this);
		mBtnGoNextActivity.setOnClickListener(this);
		strMoblile = mEtPhoneNumber.getText().toString();
		strYzm = mEtMsgYzm.getText().toString();
		mEtPhoneNumber.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				// TODO Auto-generated method stub


			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				// TODO Auto-generated method stub


			}

			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub

				if (StringAndDateUtils.isMobileNum(mEtPhoneNumber.getText().toString())) {
					// TODO 获取短信验码的按钮颜色改变

					mLlTishi.setVisibility(View.INVISIBLE);
				} else {
					mLlTishi.setVisibility(View.VISIBLE);
					mLlErrorText.setText("手机号码不正确");
				}
			}
		});
		mEtMsgYzm.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {


			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {


			}

			@Override
			public void afterTextChanged(Editable arg0) {


			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.go_next_activity_btn:
			rand = mEtMsgYzm.getText().toString();
			if (rand.length() < 4 || rand.length() > 6) {
				showToast("请输入短信验证码");
				mLlErrorText.setText("请填写验证码");
				mEtMsgYzm.requestFocus();
				mLlTishi.setVisibility(View.VISIBLE);
				return;
			}
			Bundle bundle = new Bundle();
			bundle.putString("res", res);
			bundle.putString("msgyzm", rand);
			bundle.putString("mobile", mEtPhoneNumber.getText().toString());
			actionStart(RegisterSureActivity.class, bundle);
			ActivityCollector.addActivity(RegisterActivity.this);
			break;
		case R.id.title_back_btn:
			finish();
			break;
		case R.id.get_msg_yzm_btn:
			mobile = mEtPhoneNumber.getText().toString();
			if (!StringAndDateUtils.isMobileNum(mobile)) {
				mEtPhoneNumber.requestFocus();
				mLlTishi.setVisibility(View.VISIBLE);
				mLlErrorText.setText("手机号码不正确");
				return;
			}
			recLen = 60;
			timer = new Timer();
			TimerTask task = new TimerTask() {
				@Override
				public void run() {

					runOnUiThread(new Runnable() { // UI thread

						@Override
						public void run() {
							recLen--;
							mBtnGetMsgYzm.setText("获取验证码(" + recLen + ")");
							if (recLen == 0) {
								timer.cancel();
								isLoading = false;
								mBtnGetMsgYzm.setClickable(true);
								mBtnGetMsgYzm.setBackgroundResource(R.drawable.register_getmsgyzm);
								mBtnGetMsgYzm.setText("获取验证码");
							}
						}
					});
				}
			};
			mBtnGetMsgYzm.setClickable(false);
			if (!isLoading) {
				isLoading = true;
				// register_getmsgyzm

				// .setBackgroundResource(R.drawable.register_getmsgyzm_send);

				timer.schedule(task, 1000, 1000);
				if (res.equals("regist")) {
					doSendGetMsgRequest();
				} else {
					doSendGetMsgRand();
				}

			}
			break;
		default:
			break;
		}
	}

	private void doSendGetMsgRand() {
		// TODO Auto-generated method stub

		SendPsdRand registerrand = new SendPsdRand(mobile);
		VolleyCenter.getVolley().addPostRequest(registerrand, new VolleyListenerImpl<SendPsdRandResponse>(new SendPsdRandResponse()) {

			@Override
			public void deleveryResponse(SendPsdRandResponse response) {
				if (response.isSuccess()) {
					showToast("获取验证码成功，请查收短信！");
				} else {
					showToast(response.getErrorMsg());
					mLlTishi.setVisibility(View.VISIBLE);
					mLlErrorText.setText("获取验证码失败");// 这里应该显示后台返回的失败原因

				}
			}
		});
	}

	private void doSendGetMsgRequest() {

		GetRegisterRand registerrand = new GetRegisterRand(mobile);
		VolleyCenter.getVolley().addPostRequest(registerrand, new VolleyListenerImpl<GetRegisterRandResponse>(new GetRegisterRandResponse()) {

			@Override
			public void deleveryResponse(GetRegisterRandResponse response) {
				if (response.isSuccess()) {
					showToast("获取验证码成功，请查收短信！");
				} else {
					showToast(response.getErrorMsg());
					mLlTishi.setVisibility(View.VISIBLE);
					mLlErrorText.setText("获取验证码失败");// 这里应该显示后台返回的失败原因

				}
			}
		});
	}

}
